package dao

import (
	"fmt"
	"gorm.io/gorm"
	"yundian/internel/app/helper/request"
	p "yundian/internel/app/models/permissions"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

// -------------------- dao层 - 权限规则相关操作 -----------------------//

type PermissionDao struct {

}

func NewPermissionDao() *PermissionDao {
	return &PermissionDao{}
}

// GetPermissionList 获取权限列表
func (d *PermissionDao) GetPermissionList(param *request.PermissionListReq) *dingo.ErrorWrap {
	var (
		lst    []p.PermissionsModel
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	db := global.Orm.Table("permissions")
	if param.Label != "" {
		db.Where("label like ? ", fmt.Sprint("%", param.Label, "%"))
	}
	if param.Name != "" {
		db.Where("name like ? ", fmt.Sprint("%", param.Name, "%"))
	}
	if param.Status > 0 {
		db.Where("status = ? ", param.Status)
	}

	db.Limit(limit).Offset(offset).Find(&lst).Offset(-1).Limit(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// GetPermissionListNoPager 获取权限列表
func (d *PermissionDao) GetPermissionListNoPager() *dingo.ErrorWrap {
	lst := make([]*p.PermissionsModel, 0)
	//sql := "select * from permissions"
	db := global.Orm.Table("permissions").Where("status = ?", 1).Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

// GetPermissionsFromRoleId 获取权限列表
func (d *PermissionDao) GetPermissionsFromRoleId(roleId int) *dingo.ErrorWrap {
	var m []*p.PsermissionWithActionList
	sql := "select * from permissions p " +
		"right join role_permission rp on p.id = rp.permission_id " +
		"where rp.role_id = ? and status = 1"
	db := global.Orm.Raw(sql, roleId).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}

func (d *PermissionDao) GetPermissionsByLabel(labelName string) *dingo.ErrorWrap {
	m := &p.PsermissionWithActionList{}
	sql := "SELECT p.*, rp.action_list FROM permissions p left join role_permission rp on p.id = rp.permission_id WHERE p.`label` = ?"
	db := global.Orm.Debug().Raw(sql, labelName).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}

func (d *PermissionDao) GetPermissionWithActionListFromRoleId(roleId int) *dingo.ErrorWrap {
	var m []*p.PsermissionWithActionList
	sql := "select p.*,rp.action_list from permissions p " +
		"right join role_permission rp on p.id = rp.permission_id " +
		"where rp.role_id = ?"
	db := global.Orm.Raw(sql, roleId).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}

// InsertPermission 新增权限
func (d *PermissionDao) InsertPermission(m *p.PermissionsModel) *dingo.ErrorWrap {
	db := global.Orm.Table("permissions").Create(m)
	return dingo.ErrorIf(db.Error, true)
}

// UpdatePermission 更新权限
func (d *PermissionDao) UpdatePermission(m *p.PermissionsModel) *dingo.ErrorWrap {
	db := global.Orm.Table("permissions").Model(&m).Updates(m)
	return dingo.ErrorIf(db.Error, true)
}

// DeletePermission 删除权限
func (d *PermissionDao) DeletePermission(id int) *dingo.ErrorWrap {
	err := global.Orm.Transaction(func(tx *gorm.DB) error {
		// 1. 删除权限表
		tx = tx.Exec("delete from permissions where id = ?", id)
		if err := tx.Error; err != nil {
			return err
		}
		// 2. 删除角色路由关联表
		tx.Exec("delete from role_permission where permission_id = ?", id)
		if err := tx.Error; err != nil {
			return err
		}
		// 返回 nil 提交事务
		return nil
	})
	return dingo.ErrorIf(err, true)
}
